 **
 **                SEGURANCA
 **
 local mcabeca[6],mestrutura[6]
 mretencao := rvalor := 0
 sele notass  
 go bottom
 skip -10
 limpa(3,0,24,80,cor,.f.)
 limpa(3,0,23,79,cor,.t.)
 mcabeca[1] = 'NOTA FISCAL' ; mestrutura[1] = fiel(1)
 mcabeca[2] = 'EMISSAO'     ; mestrutura[2] = fiel(3)
 mcabeca[3] = 'VENCTO'      ; mestrutura[3] = fiel(4)
 mcabeca[4] = 'RECBTO'      ; mestrutura[4] = fiel(21)
 mcabeca[5] = 'TOTAL'       ; mestrutura[5] = fiel(20)
 mcabeca[6] = 'I'           ; mestrutura[6] = fiel(19)
 menu(24,0,' Inclui ',2,1)
 menu(24,8,'Altera ',1,1)
 menu(24,15,'Exclui ',1,1)
 menu(24,22,'Procura ',1,1)
 menu(24,30,'Boleto ',1,1)
 menu(24,37,'Relatorios ',1,1)
 menu(24,48,'GPS ',1,1)
 menu(24,52,'Recebimento ',3,1)
 menu(24,64,'Nota ',3,1)
 menu(24,72,'Esc-Sai ',1,3)
 dbedit(4,1,22,78,mestrutura,'segura100','@X',mcabeca,.t.)
retu


function segura100(modo,coluna)
set cursor on
mcliente = cliente
@ 3,0 say space(80) color 'w+/r'
sele clientes
seek mcliente
if .not. eof()
  @ 3,0 say ' Cliente : ' + str(mcliente,6) + ' ' + nome color 'w+/r'
endif
sele notass

if modo = 0 .or. modo = 3
   retu(1)
endif
tecla = lastkey()
do case
   case tecla = 5 .and. modo = 1

    mensa(10,'Inicio do arquivo ',2)
    retu(2)

   case tecla = 24 .and. modo = 2

    mensa(10,' Final do arquivo ',2)
    retu(2)

   case tecla = 27

    msaida = .t.
    retu(0)


   case tecla = 73 .or. tecla = 105  
                          
        xinss      = right(dtoc(date()),7)
        telageral := savescreen(0,0,24,80)
        sele notass
        while .t.
         legal(0,0,24,79,'Inclusao')
         sele notass 
         go bottom
         mnumero = numero+1
         @ 1,2  say 'N.F. Numero   :' get mnumero pict '999999'
         read
         if empty(mnumero) .or. lastkey() = 27
            exit
         endif
         seek mnumero
         if .not. eof()
            mensa(10,'Nota Fiscal ja cadastrada',2)
            loop
         endif
         inicializa()
         minss    = xinss
         mcliente = 0
         @ 2,2 say 'Cliente       :' get mcliente  pict '999999'
         read
         sele clientes
         seek mcliente
         if eof()
            telalocal = savescreen(0,0,24,80)
            do clientes with .t.
            mcliente = codigo
            restscreen(0,0,24,80,telalocal)
         endif
         mirrf   = ir
         mcsll   = cs
         mpis    = pis
         mcofins = cofins
         @ 2,2  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + left(nome,50)
         sele notass
         @ 1,50 say 'Competencia   :' get minss     pict '99/9999'
         @ 3,2  say 'Emissao       :' get memissao
         @ 3,40 say 'Vencimento    :' get mvencto  
         read
         mlinha     = 3
         mmodelo    = 0
         maliqiss   = 5
         legal(10,4,14,30,'Busca NF Modelo')
         @ 12,6 say 'Nota :' get mmodelo pict '999999'
         read
         if empty(mmodelo)
            declare mdescricao[12], munitario[12], mvalor[12]
            afill(mdescricao,space(37))
            afill(munitario,0)
            afill(mvalor,0)
         else
            sele itemsegu
            seek mmodelo
            if eof()
               mensa(10,'Nota fiscal nao encontrada',2)
               loop
            endif
            mdescricao = {}
            munitario  = {}
            mvalor     = {}    
            for x = 1 to 12
                aadd(mdescricao,descricao)
                aadd(munitario,unitario)
                aadd(mvalor,valor)
                skip
            next  
            sele notass
            seek mmodelo
            if eof()
               mensa(10,'NF nao',2)
            endif
            mretencao1 = retencao1
            mretencao2 = retencao2
            mretencao3 = retencao3
            mbanco     = banco
            mmaterial  = material
         endif
         for x = 1 to 12
             @ mlinha+x,2  get mdescricao[x] pict '@!'
             @ mlinha+x,44 get munitario[x]  pict '999,999,999.99'
             @ mlinha+x,61 get mvalor[x]     pict '999,999,999.99'
         next
         read
         mtotal = 0
         for x = 1 to 12
             mtotal+=mvalor[x]
         next  
         @ 16,2 say 'Total da Nota Fiscal ' + space(40) + trans(mtotal,'9,999,999.99') color 'w+/r'
         @ 17,2  say 'Transporte :' get mvaletransp pict '999,999.99'
         @ 17,28 say 'Refeicao :'   get mvalealimen pict '999,999.99'
         @ 17,54 say 'Material :'   get mvmaterial   pict '999,999.99'
         read
         teste(18,2,mretencao1)
         mretencao1 = mretencao
         mvalorret1 = rvalor
         teste(19,2,mretencao2)
         mretencao2 = mretencao
         mvalorret2 = rvalor
         teste(20,2,mretencao3)
         mretencao3 = mretencao
         mvalorret3 = rvalor
         sele notass
         @ 21,02 say 'IRRF : ' + trans(mirrf,'99.99')
         @ 21,15 say 'CSLL : ' + trans(mcsll,'99.99')
         @ 21,28 say 'COFINS : ' + trans(mcofins,'99.99')
         @ 21,43 say 'PIS : ' + trans(mpis,'99.99')
         mtemp      = mirrf + mcsll + mpis + mcofins
         mfederal   = mtotal * (mtemp / 100)
         @ 21,57 say  trans(mtemp,'99.99') + trans(mfederal,'99,999,999.99')
         mliquido = mtotal - (mvalorret1 + mvalorret2 + mvalorret3 + mfederal)
         @ 22,2  say 'Total liquido ' + space(48) + trans(mliquido,'9,999,999.99') color 'w+/r'
         @ 23,2  say 'Banco      :' get mbanco pict '99'
         @ 23,20 say 'Material :' get mmaterial pict '!' valid mmaterial $ 'SN'
         read
         sele bancos
         seek mbanco
         if eof()                     
            telabanco = savescreen(0,0,24,80)
            do bancos with .t.
            mbanco = codigo
            restscreen(0,0,24,80,telabanco)
         endif
         @ 24,20 say alltrim(nome) + ' Agencia : ' + alltrim(agencia) + ' C/C : ' + alltrim(conta)
         sele notass
         @ 23,36 say 'Aliq ISS :' get maliqiss pict '99'
         read
         if mretencao1 # 3 .and. mretencao2 # 3 .and. mretencao3 # 3
            miss = mtotal * (maliqiss/100)
            @ 23,55 say 'I.S.S.   : ' + trans(miss,'999,999.99')
         endif
         if confirma(4,60,' 1 - Confirma ',' 2 - Cancela  ')
            appe blank
            if inclusao()
               grava()
               unlock
               sele itemsegu
               for x = 1 to 12
                   appe blank
                   repla nota      with mnumero
                   repla descricao with mdescricao[x]
                   repla unitario  with munitario[x]
                   repla valor     with mvalor[x]
                   unlock
               next  
            endif
         endif 
        enddo
        restscreen(0,0,24,80,telageral)
        sele notass   
        go top
        retu(2)
   
   case tecla = 65 .or. tecla = 97   

        telageral := savescreen(0,0,24,80)
        while .t.
         sele notass 
         legal(0,0,24,79,'Alteracao de Nota Fiscal')
         mnumero := numero         
         @ 1,2  say 'N.F. Numero   :' get mnumero pict '999999'
         read
         if empty(mnumero) .or. lastkey() = 27
            exit
         endif
         seek mnumero
         if eof()
            mensa(10,'Nota Fiscal nao cadastrada',2)
            loop
         endif
         inicializa()
         @ 2,2  say 'Cliente       :' get mcliente  pict '999999'
         read
         sele clientes
         seek mcliente
         if eof()
            telalocal = savescreen(0,0,24,80)
            do clientes with .t.
            mcliente = codigo
            restscreen(0,0,24,80,telalocal)
         endif
         @ 2,2  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + left(nome,50)
         sele notass
         @ 1,50 say 'Competencia   :' get minss     pict '99/9999'
         @ 3,2  say 'Emissao       :' get memissao
         @ 3,30 say 'Vencimento :' get mvencto  
         @ 3,60 say 'Impressa   :' get mimpressa pict '!' valid mimpressa $ 'SN'
         read
         sele itemsegu
         seek mnumero
         mlinha     = 3 
         mdescricao = {}
         munitario  = {}
         mvalor     = {}
         for x = 1 to 12
             aadd(mdescricao,descricao)
             aadd(munitario,unitario)
             aadd(mvalor,valor)
             @ mlinha+x,2  get mdescricao[x] pict '@!'
             @ mlinha+x,44 get munitario[x]  pict '999,999,999.99'
             @ mlinha+x,61 get mvalor[x]     pict '999,999,999.99'
             skip
         next
         read
         mtotal = 0
         for x = 1 to 12
             mtotal+=mvalor[x]
         next
         @ 16,2 say 'Total da Nota Fiscal ' + space(40) + trans(mtotal,'9,999,999.99') color 'w+/r'
         @ 17,2  say 'Transporte :' get mvaletransp pict '999,999.99'
         @ 17,28 say 'Refeicao :'   get mvalealimen pict '999,999.99'
         @ 17,54 say 'Material :'   get mvmaterial  pict '999,999.99'
         read
         teste(18,2,mretencao1)
         mretencao1 = mretencao
         mvalorret1 = rvalor
         teste(19,2,mretencao2)
         mretencao2 = mretencao
         mvalorret2 = rvalor
         teste(20,2,mretencao3)
         mretencao3 = mretencao
         mvalorret3 = rvalor
         sele notass
         @ 21,02 say 'IRRF : ' + trans(mirrf,'99.99')
         @ 21,15 say 'CSLL : ' + trans(mcsll,'99.99')
         @ 21,28 say 'COFINS : ' + trans(mcofins,'99.99')
         @ 21,43 say 'PIS : ' + trans(mpis,'99.99')
         mtemp      = mirrf + mcsll + mpis + mcofins
         mfederal   = mtotal * (mtemp / 100)
         @ 21,57 say  trans(mtemp,'99.99') + trans(mfederal,'99,999,999.99')

         mliquido = mtotal - (mvalorret1 + mvalorret2 + mvalorret3 + mfederal)
         @ 22,2 say 'Total liquido        ' + space(40) + trans(mliquido,'9,999,999.99') color 'w+/r'
         @ 23,2  say 'Banco      :' get mbanco pict '99'
         @ 23,20 say 'Material :' get mmaterial pict '!' valid mmaterial $ 'SN'
         read
         sele bancos
         if .not. empty(mbanco)
            seek mbanco
            if eof()
               mensa(10,'Banco nao cadastrado',2)
               loop
            endif
            @ 24,20 say alltrim(nome) + ' Agencia : ' + alltrim(agencia) + ' C/C : ' + alltrim(conta)
         endif
         sele notass
         @ 23,36 say 'Aliq ISS :' get maliqiss pict '99'
         read
         if mretencao1 # 3 .and. mretencao2 # 3 .and. mretencao3 # 3
            miss = mtotal * (maliqiss/100)
            @ 23,55 say 'I.S.S.  : ' + trans(miss,'999,999.99')
         else
            miss = 0
         endif
         if confirma(4,60,' 1 - Confirma ',' 2 - Cancela  ')
            if inclusao()
               grava()
               unlock
            endif
            sele itemsegu
            seek mnumero
            for x = 1 to 12
                repla descricao with mdescricao[x]
                repla unitario  with munitario[x]
                repla valor     with mvalor[x]
                unlock
                skip
            next  
         endif 
        enddo
        restscreen(0,0,24,80,telageral)
        sele notass  
        go top
        retu(2)
 
   case tecla = 78 .or. tecla = 110             // Impressao Nota Fiscal
                                     
        tlocal = savescreen(0,0,24,80)
        do novaseg
        restscreen(0,0,24,80,tlocal)
        sele notass
        retu(2)

   
   case tecla = 66 .or. tecla = 98              // Impressao boleto     
                                     
        sele notass   
        if .not. impressora()
           retu(2)
        endif
        while .t.
         legal(8,4,22,77,'Impressao de Boleto Bancario')
         sele notass  
         mnota = numero
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         seek mnota
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mcliente = cliente
         mvencto  = vencto
         memissao = emissao
         sele clientes
         seek mcliente
         mnome   = nome
         mretiss = retiss
         if boleto = 'N'
            mensa(10,'Cliente nao utiliza boleto bancario',2)
            mnota+=1
            sele notass
            skip
            loop
         endif
         sele notass
         mtotal   = total - (valorret1 + valorret2 + valorret3 + federal)
         if mretiss = 'S'
            mtotal -= iss
         endif
         mmora    = 2.5
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,6  say 'Valor         :' + trans(mtotal,'999,999,999.99')
         @ 12,40 say 'Vencimento    :' get mvencto     
         @ 13,6  say 'Mora          :' get mmora pict '99.99'
         read
         mmora = mtotal * (mmora/100)
         mmora = mmora / 30
         mlinha1 := mlinha2 := space(40)
         @ 14,6  say 'Mora          :' + trans(mmora,'999,999.99')
         @ 15,6  say 'Obs-1         :' get mlinha1 pict '@!'
         @ 16,6  say 'Obs-2         :' get mlinha2 pict '@!'
         read
         if .not. confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            exit
         endif
         sele notass  
         set devi to printer
         @ prow()+1,50 say mvencto
         @ prow()+3,0  say memissao                                  
         @ prow(),16   say mnota     
         @ prow(),26   say 'NF'                              
         @ prow()+2,50 say mtotal pict '999,999.99'
         @ prow()  ,50 say mtotal pict '999,999.99'
         @ prow()  ,50 say mtotal pict '999,999.99'
         @ prow()+2,0  say 'Cobrar juros de R$ ' + trans(mmora,'9999.99') + ' por dia de atraso.'
         @ prow()+1,0  say mlinha1
         @ prow()+1,0  say mlinha2
         @ prow()+2,2  say ''
         sele clientes
         @ prow()+3,2  say nome                        
         @ prow()+1,2  say endereco
         @ prow()+1,2  say alltrim(cidade) + ' - ' + uf + space(20) + trans(cgc,'@R 99.999.999/9999-99')  
         @ prow()+7,2  say ''
         set devi to screen
         sele notass
         skip
        enddo
        sele notass
        skip
        retu(2)
   
   case tecla = 82 .or. tecla = 114                                     
                                     
        telageral := savescreen(0,0,24,80)
        do relatos
        restscreen(0,0,24,80,telageral)
        sele notass
        go top
        retu(2)

   case tecla = 71 .or. tecla = 103             // GPS
                                     
        sele notass   
        if .not. impressora()
           retu(2)
        endif
        mvencto  = ctod('')
        while .t.
         legal(8,4,22,77,'Impressao da G.P.S.')
         sele notass  
         mnota = 0        
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         seek mnota
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mcompet  = right(dtoc(emissao),7)
         mcliente = cliente
         sele clientes
         seek mcliente
         mnome = nome
         sele notass
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,6  say 'Valor         :' + trans(total,'999,999,999.99')
         @ 12,40 say 'Vencimento    :' get mvencto     
         @ 13,6  say 'Competencia   :' get mcompet pict '99/9999'
         read
         if confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            set devi to printer
            @ prow()+1,60 say '2631'   
            @ prow()+2,60 say mcompet                                   
            @ prow()+2,54 say '03.497.401/0001-97'
            @ prow()+1,58 say valorret1 pict '999,999.99'
            @ prow()+1,2  say 'BRASFORT EMPRESA DE SEGURANCA LTDA'    
            @ prow()+1,2  say 'SAAN QD. 01 No. 635 - BRASILIA'          
            @ prow()+1,2  say left(mnome,40)
            @ prow()  ,2  say left(mnome,40)
            @ prow()  ,2  say left(mnome,40)
            @ prow()+8,2  say 'Retencao da Nota Fiscal : ' + strzero(mnota,6)
            @ prow()+7,2  say ''
            set devi to screen
         endif
         skip
        enddo
        sele notass
        skip
        retu(2)
   
   
   case tecla = 67 .or. tecla = 99              // Recebimento          

        sele notass
        while .t.
         mnota = 0
         legal(8,4,22,77,'Recebimento de Nota Fiscal')
         @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
         read
         if empty(mnota) .or. lastkey() = 27
            exit
         endif
         seek mnota
         if eof()
            mensa(10,'Nota fiscal nao cadastrada',2)
            loop
         endif
         mrecebto = recebto
         mcancela = cancela
         mcliente = cliente
         sele clientes
         seek mcliente
         mnome = nome
         sele notass  
         @ 11,6  say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,6  say 'Valor         :' + trans(total,'999,999,999.99')
         @ 13,6  say 'Recebimento   :' get mrecebto    
         @ 14,6  say 'Cancela       :' get mcancela pict '!' valid mcancela $ 'SN'
         read
         if confirma(18,60,' 1 - Confirma ',' 2 - Cancela  ')
            if alteracao()
               repla recebto with mrecebto
               repla cancela with mcancela
               unlock
            endif
         endif
        enddo
        retu(2)

   case tecla = 80 .or. tecla = 112                                 
                                     
        mnota = 0
        legal(8,4,22,77,'Procura Nota Fiscal')
        @ 10,6 say 'Nota-Fiscal   :' get mnota pict '999999'
        read
        locate for mnota = numero
        retu(2)

   case tecla = 69 .or. tecla = 101         // exclusao            

        legal(8,8,22,70,'Exclusao de Nota Fiscal')
        mnota = 0
        @ 10,10 say 'Nota Fiscal :' get mnota pict '999999'
        read
        seek mnota
        if .not. eof()
         mcliente = cliente
         sele clientes
         seek mcliente
         mnome = nome
         sele notass
         @ 11,10 say 'Cliente       : ' + strzero(mcliente,6) + ' ' + mnome
         @ 12,10 say 'Valor         :' + trans(total,'999,999,999.99')
         if .not. confirma(18,50,' 1 - Cancela  ',' 2 - Confirma ')
            if alteracao()
               dele
               unlock
            endif
            sele itemsegu
            go top
            while .not. eof()
             if mnota = nota
                if alteracao()
                   dele
                   unlock
                endif
             endif
             skip
            enddo
         endif
        endif
        sele notass
        retu(2)

     otherwise

        retu(2)

endcase


